<?php

/**
 * IndexController - The default controller class
 *
 * @author
 * @version
 */

require_once 'Zend/Controller/Action.php';

class Administrator_FenleiController extends Zend_Controller_Action
{
    public function init()
    {

        $member=new CMember();

        if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName()))
        {

            $this->_redirect(MY_URL.'/administrator-auth-login');

        }
    	$memberArray = $member->getUserDetails(array('uid' => $_COOKIE[COOKIE_PRE.'uid']));
    	if($_COOKIE[COOKIE_PRE.'uid']=='1'){
				
		} else {
        	$strarray = explode(",", $memberArray[0]['menuflag']);
        	
        	if (!in_array('6', $strarray)) {
        		exit('no priviage');
        	}
		}

    }

    public function preDispatch()
    {

        $this->getHelper ('layout')->setLayout('framelayout');

        $this->view->addHelperPath(MY_ROOT.'/vhelpers');

        Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');

    //$this->getHelper('User')->getinfo($this->view,$this->_request->getModuleName(),$this->_request->getControllerName());

    }

    public function indexAction()
    {

        $type=$this->_request->getParam('type');

        $typefl=new CFenlei($type);

        $fenleiArray=$typefl->getAllFenleiByOrder();

        $typefl->getAllFenleiManagement($fenleiArray,0);

        $fenleiString=str_replace(array('<div class="smallChildDiv"></div>','<div></div>'),array('',''),$typefl->fenleiString);
        $this->view->assign('fenleiString',$fenleiString);

        $this->view->assign('type',$type);

    }
    public function addAction()
    {

    }
    public function doaddAction()
    {
        $this->_helper->viewRenderer->setNoRender();

        $paramArray=$this->_request->getParams();

        $filters = array(
            '*'   => 'StringTrim'
        );

        $validators = array(
        );

        $input = new Zend_Filter_Input($filters, $validators, $paramArray);


		/*图片附件 start*/
        $logostr = $_POST[imgUrl];
		/*图片附件 end*/

        $content=htmlspecialchars_decode($_POST[flbody]);

        $content=str_replace('html/upload/temp_img/','html/upload/item_img/',$content);

        $fenlei=new CFenlei("product");

		$data=array(
            'flname'=>trim($_POST[flname]),
            'en_flname'=>trim($_POST[en_flname]),
            'flbody'=>trim($content),
            'openflag'=>trim($_POST[openflag]),
//            'badgepatch'=>trim($_POST[badgepatch]),
            'Printnumber'=>trim($_POST[Printnumber]),
            'logostr'=>trim($logostr[count($logostr)-1]),
			'fenlei_keywords'=>trim($_POST['fenlei_keywords']),
			'title'=>trim($_POST['title']),
			'fenlei_description'=>trim($_POST['description'])
        );
        $fenlei->createFenlei($data);

        $this->view->showMsg("添加成功!",'administrator-fenlei-index?type=product');

        return true;
    }
    public function editAction()
    {

        $itemid=$this->_request->getParam('itemid');

        $CFenlei=new CFenlei("product");

        $oneFenleiArray=$CFenlei->getoneFenlei($itemid);

        $lagnappeselectArray=$CFenlei->getFenleiByPid('213');

        $lengthstature = unserialize($oneFenleiArray[0]['lengthstature']);
        $this->view->assign('lengthstature',is_array($lengthstature)?$lengthstature:array());
        
        $this->view->assign('oneFenleiArray',$oneFenleiArray);

        $this->view->assign('lagnappeselectArray',$lagnappeselectArray);
        
        //获取配件信息
        $accessory = new CProductaccessory();
        $info = $accessory->getFenleiAccessory($itemid);
        foreach($info as $k=>$v){
        	$accessoryInfo[$k]=$accessory->getAccessoryInfo($v['accessoryid'],'',$v['flid']);
        }
        $this->view->assign('info',is_array($accessoryInfo)?$accessoryInfo:array());
        
        $flid=342;
        $item=new CItem();
        $bindProductArray=$item->getitemidFlid($flid);
        
        foreach($info as $k=>$v){
        	foreach($bindProductArray as $key=>$value){
        		if($v['accessoryid']==$value['itemid']){
        			unset($bindProductArray[$key]);
        		}
        	}
        }
        $this->view->assign('bindProductArray',is_array($bindProductArray)?$bindProductArray:array());

    }

     public function sizeurlAction()
    {

        $itemid=$this->_request->getParam('itemid');

        $CFenlei=new CFenlei("product");

        $oneFenleiArray=$CFenlei->getoneFenlei($itemid);

        $lagnappeselectArray=$CFenlei->getFenleiByPid('213');

        // die($oneFenleiArray[0]["flname"]."a");

        $this->view->assign('oneFenleiArray',$oneFenleiArray);

        $this->view->assign('lagnappeselectArray',$lagnappeselectArray);

    }

    public function doeditAction()
    {
        $this->_helper->viewRenderer->setNoRender();

        if(!$this->getRequest()->isPost())
        {

            $this->view->showMsg("发生未知错误!");

            return false;
        }

        $paramArray=$this->_request->getParams();

        $itemid=$paramArray['itemid'];

        $filters = array(
            '*'   => 'StringTrim'
        );

        $validators = array(
        );

        $input = new Zend_Filter_Input($filters, $validators, $paramArray);

		/*图片附件 start*/
        $logostr = $_POST[imgUrl];
		/*图片附件 end*/

        $content=htmlspecialchars_decode($_POST[flbody]);

        $content=str_replace('html/upload/temp_img/','html/upload/item_img/',$content);


         	/*修改公告表 start*/

        $fenlei=new CFenlei("product");
        
        /* lengthstature */
        $lengthstature = array();
        $params_str = '';
		foreach ($paramArray as $k=>$v) { $params_str .= $k . ','; }
		preg_match_all("/lengthstature_size_(\d+)/",$params_str,$match);
		if(!empty($match[1]))
		{
			foreach ($match[1] as $v) {
				$temp = $_POST['lengthstature_param_'.$v];
				$lengthstature[$_POST['lengthstature_size_'.$v]] = array(
	        	'cm'=>array(
	        		'length'	=> $temp[0],
	        		'stature'	=> $temp[1],
	        	),
	        	'inch'=>array(
	        		'length'	=> stripslashes($temp[2]),
	        		'stature'	=> stripslashes($temp[3])
	        	));
	        	unset($temp);
			}
		}
        /* lengthstature */

        $data=array(
            'flname'=>trim($_POST[flname]),
            'en_flname'=>trim($_POST[en_flname]),
            'flbody'=>trim($content),
            'openflag'=>trim($_POST[openflag]),
        	'fenlei_keywords'=>trim($_POST['fenlei_keywords']),
        	'title'=>trim($_POST['title']),
        	'fenlei_description'=>trim($_POST['fenlei_description']),
//            'badgepatch'=>trim($_POST[badgepatch]),
            'Printnumber'=>trim($_POST[Printnumber]),
            'logostr'=>trim($logostr[count($logostr)-1]),
        	'lengthstature'=>serialize($lengthstature),
        );

        ///die($_POST[flname]);
        $fenlei->editFenlei($itemid,$data);
        
        $accessory = new CProductaccessory();
        $accessory->delFlidAccessory($itemid);
        if(is_array($_POST['bind2'])){
        	$accessorydata['itemid']=0;
        	$accessorydata['flid']=$itemid;
        	foreach($_POST['bind2'] as $v){
        		$arr = explode("%%%", $v);
        		$accessorydata['accessoryid']=$arr[0];
        		$accessorydata['price']=$arr[1];
        		$accessory->createProductAccessory($accessorydata);
        	}
        }

		/*修改公告表 end*/

        $this->view->showMsg("修改成功!",'administrator-fenlei-index?type=product');

        return true;

    }

    public function dosizeurlAction()
    {

        $this->_helper->viewRenderer->setNoRender();

        if(!$this->getRequest()->isPost())
        {

            $this->view->showMsg("发生未知错误!");

            return false;
        }

        $paramArray=$this->_request->getParams();

        $itemid=$paramArray['itemid'];



		/*图片附件 start*/
        $sizepicurl = $_POST[imgUrl];
		/*图片附件 end*/

         	/*修改公告表 start*/

        $fenlei=new CFenlei("product");

        $data=array(
            'sizepicurl'=>trim($sizepicurl[count($sizepicurl)-1])
        );

        ///die($_POST[flname]);
        $fenlei->editFenlei($itemid,$data);

		/*修改公告表 end*/

        $this->view->showMsg("修改成功!",'administrator-fenlei-index?type=product');

        return true;

    }
    
    public function alterAction()
    {

        $this->_helper->viewRenderer->setNoRender();

        $thisArray=$this->_request->getParams();

        if(trim($thisArray['flname'])!='')
        {

            $type=$thisArray['type'];

            $fenlei=new CFenlei($type);

            $fenlei->editFenlei($thisArray['flid'],array('flname'=>trim($thisArray['flname']),'en_flname'=>trim($thisArray['en_flname']),'flbody'=>trim($thisArray['flbody']),'openflag'=>trim($thisArray['openflag'])));

            $this->_forward('index');

        }

    }

    public function newAction()
    {

        $this->_helper->viewRenderer->setNoRender();

        $thisArray=$this->_request->getParams();

        if(trim($thisArray['flname'])!='')
        {

            $type=$thisArray['type'];

            $fenlei=new CFenlei($type);

			if ($thisArray['type'] == 'product') {
				$flid = $fenlei->createFenlei(array('flname'=>trim($thisArray['flname']),'en_flname'=>trim($thisArray['en_flname']),'flbody'=>trim($thisArray['flbody']),'openflag'=>trim($thisArray['openflag']),'logostr'=>'','pid'=>$thisArray['flid'],'fenlei_keywords'=>$thisArray['keywords'], 'fenlei_description'=>$thisArray['description'], 'title'=>$thisArray['title']));
			} else {
            	$flid = $fenlei->createFenlei(array('flname'=>trim($thisArray['flname']),'en_flname'=>trim($thisArray['en_flname']),'flbody'=>trim($thisArray['flbody']),'openflag'=>trim($thisArray['openflag']),'logostr'=>'','pid'=>$thisArray['flid']));
			}
			//添加配件
			$Caccessory = new CProductaccessory();
			if(is_array($thisArray['interrelated'])){
				$accessorydata['flid']=$flid;
				$accessorydata['itemid']=0;
				foreach($thisArray['interrelated'] as $v){
					$arr = explode("%%%", $v);
					$accessorydata['accessoryid']=$arr[0];
					$accessorydata['price']=$arr[1];
					$Caccessory->createProductAccessory($accessorydata);
				}
			}
            $this->_forward('index');
        }

    }

    public function dodelAction()
    {

        $this->_helper->viewRenderer->setNoRender();

        $thisArray=$this->_request->getParams();

        if(is_numeric($thisArray['flid']))
        {

            $type=$thisArray['type'];

            $fenlei=new CFenlei($type);

            $fenlei->delFenlei($thisArray['flid']);
            //删除分类对应配件
            $Caccessory = new CProductaccessory();
            $Caccessory->delFlidAccessory($thisArray['flid']);

            $this->_forward('index');
        }

    }
    public function getaccessoryAction(){
    	$item = new CItem();
        $flid= 342;
        $accessoryProductArray=$item->getitemidFlid($flid);
        echo json_encode($accessoryProductArray);
    	exit();
    }


    public function postAction()
    {

    //$this->_helper->viewRenderer->setNoRender();

    //$this->getHelper ('layout')->disableLayout ();

    }



}
